//
//  TMGCultureHomeHeader.m
//  ligenyun
//
//  Created by Mac on 2018/3/27.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "TMGCultureHomeHeader.h"
@interface TMGCultureHomeHeader()
@property(nonatomic, strong) NSMutableArray                     *jiaoliuButtonArray;
@property(nonatomic, strong) NSMutableArray                     *baohuButtonArray;
@end
@implementation TMGCultureHomeHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubView];
    }
    return self;
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    bannerImageView.image = [UIImage imageNamed:@"文化交流banner"];
    [self addSubview:bannerImageView];
    
    UIView * jiaoliuVIew = [TMUtils headerViewWithTitle:@"文化交流" moreButtonAction:^(UIButton *sender) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(cultureHomeHeader:onSelctedCommunicateArea:)]) {
            [self.delegate cultureHomeHeader:weakSelf onSelctedCommunicateArea:@"文化交流"];
        }
    }];
    jiaoliuVIew.top = bannerImageView.bottom;
    [self addSubview:jiaoliuVIew];
    
    UIImageView * jiaoliuImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, jiaoliuVIew.bottom, UIScreenWidth, 93)];
    jiaoliuImageView.image = [UIImage imageNamed:@"礼根文化bg"];
    jiaoliuImageView.userInteractionEnabled = YES;

    [self addSubview:jiaoliuImageView];
    
    CGFloat buttonWith = (UIScreenWidth - 100)/3;
    NSArray * array = @[@"各朝朝代祭祀礼",@"各名族祭祀礼",@"大型祭祀活动",@"名俗博物馆"];
    self.jiaoliuButtonArray = [NSMutableArray new];
    for (NSInteger i = 0; i<array.count; i++) {
        NSString * title = array[i];
        STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(25, 10, buttonWith, 30)
                                                         title:title
                                                    titleColor:SecendTextColor
                                                     titleFont:12
                                                  cornerRadius:4
                                               backgroundColor:FlatGray
                                               backgroundImage:nil
                                                         image:nil];
        buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [buyButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelctedJiaoLiuButton:sender];
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(cultureHomeHeader:onSelctedCommunicateArea:)]) {
                [self.delegate cultureHomeHeader:weakSelf onSelctedCommunicateArea:sender.currentTitle];
            }
        }];
        [self.jiaoliuButtonArray addObject:buyButton];
        if (i == 0) {
            [self onSelctedJiaoLiuButton:buyButton];
        }
       [buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        buyButton.left =25 + 25* (i%3) + buttonWith * (i%3);
        buyButton.top = 10 +10 * (i/3) + 30 * (i/3);
        [jiaoliuImageView addSubview:buyButton];
    }
    
    
    UIView * bahuVIew = [TMUtils headerViewWithTitle:@"文化保护" moreButtonAction:^(UIButton *sender) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(cultureHomeHeader:onSelctedCommunicateArea:)]) {
            [self.delegate cultureHomeHeader:weakSelf onSelctedProductArea:@"文化保护"];
        }
    }];
    bahuVIew.top = jiaoliuImageView.bottom;
    [self addSubview:bahuVIew];
    
    UIImageView * bahuImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, bahuVIew.bottom, UIScreenWidth, 93)];
    bahuImageView.image = [UIImage imageNamed:@"礼根文化bg"];
    bahuImageView.userInteractionEnabled = YES;
    [self addSubview:bahuImageView];
    
//    CGFloat buttonWith = (UIScreenWidth - 100)/3;
    NSArray * baohuarray = @[@"各朝朝代祭祀礼",@"各名族祭祀礼",@"大型祭祀活动",@"名俗博物馆"];
    self.baohuButtonArray = [NSMutableArray new];
    for (NSInteger i = 0; i<baohuarray.count; i++) {
        NSString * title = array[i];
        STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(25, 10, buttonWith, 30)
                                                         title:title
                                                    titleColor:SecendTextColor
                                                     titleFont:12
                                                  cornerRadius:4
                                               backgroundColor:FlatGray
                                               backgroundImage:nil
                                                         image:nil];
        buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [buyButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelctedBaohuButton:sender];
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(cultureHomeHeader:onSelctedProductArea:)]) {
                [self.delegate cultureHomeHeader:weakSelf onSelctedProductArea:sender.currentTitle];
            }
        }];
        [self.baohuButtonArray addObject:buyButton];
        
        if (i == 0) {
            [self onSelctedBaohuButton:buyButton];
        }
        [buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        buyButton.left = 25 + 25* (i%3) + buttonWith * (i%3);
        buyButton.top = 10+ 10 * (i/3) + 30 * (i/3);
        [bahuImageView addSubview:buyButton];
    }
    
    
    NSArray * bootomArray = @[@"寻根文化",@"家谱文化",@"百家起源",@"风水文化"];
    for (NSInteger i = 0; i < bootomArray.count; i ++ ) {
        NSString * title = bootomArray[i];
        STMenuControl * menu = [[STMenuControl alloc] initWithFrame:CGRectMake(0, 120, UIScreenWidth / 4, UIScreenWidth / 4) imageName:title title:title titleColor:SecendTextColor];
        [bahuImageView addSubview:menu];
        menu.left = i * (UIScreenWidth / 4);
        bahuImageView.height = menu.bottom;
        [menu setOnSelctedControl:^(STMenuControl *control) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(cultureHomeHeader:onSelctedProductArea:)]) {
                [self.delegate cultureHomeHeader:weakSelf onSelctedProductArea:control.titleLable.text];
            }
        }];
    }
    
    
    self.height = bahuImageView.bottom;
    
    
}
#pragma mark --Action Method
- (void)onSelctedJiaoLiuButton:(UIButton*)sender{
    for (UIButton * button in self.jiaoliuButtonArray) {
        
        button.selected = NO;
        sender.selected = YES;
        if (!button.selected) {
            button.backgroundColor = FlatGray;
        }else{
            button.backgroundColor = TM_redColor;
        }
    }
    

}
- (void)onSelctedBaohuButton:(UIButton*)sender{
    for (UIButton * button in self.baohuButtonArray) {
        
        button.selected = NO;
        sender.selected = YES;
        if (!button.selected) {
            button.backgroundColor = FlatGray;
        }else{
            button.backgroundColor = TM_redColor;
        }
    }
}
@end
